package com.lxx.onlineview.mapper;

import com.lxx.onlineview.entity.po.Teach;
import com.lxx.onlineview.utils.ObjectUtil;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;

import java.util.Map;

public class TeachProvider {
    private static final String TABLE_NAME = "teach";

    public String update(@Param("teach") Teach teach) {
        return new SQL() {{
            UPDATE(TABLE_NAME);
            try {
                Map<String, Object> data = ObjectUtil.reflect(teach);
                data.forEach((key, value) -> {
                    if (value != null) {
                        SET(key + " = #{" + key + "}");
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
            }
            WHERE("name = #{name}");
        }}.toString();
    }

    public String insert(@Param("teach") Teach teach) {
        return new SQL() {{
            INSERT_INTO(TABLE_NAME);
            try {
                Map<String, Object> data = ObjectUtil.reflect(teach);
                data.forEach((key, value) -> {
                    if (value != null) {
                        VALUES(key, "\'"+value.toString()+"\'");
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
            }
        }}.toString();
    }
}
